package at.fhooe.mcm.logic.logging;

import java.io.OutputStream;
import java.io.PrintStream;

import at.fhooe.mcm.logic.core.Core;

public class ConsoleLoggingService extends LoggingService
{

	public static final String	NAME	= "logging.console";
	
	private PrintStream		m_ps;
	
	public static ConsoleLoggingService getInstance(Core _core)
	{
		return (ConsoleLoggingService) _core.getService(NAME);
	}

	public ConsoleLoggingService(int _level)
	{
		m_ps = System.out;
		m_level = _level;
	}

	public ConsoleLoggingService()
	{
		this(WARNING);
	}

	@Override
	public String getName()
	{
		return NAME;
	}

	@Override
	protected void log(int _level, String _source, String _message)
	{
		if(_level > m_level)
		{
			return;
		}
		
		m_ps.print(timeToString(m_core.getTime()));
		m_ps.print(" " + levelToText(_level));
		
		if (_source != null)
		{
			m_ps.print(" [" + _source + "]");
		}
		
		m_ps.print(": ");

		if (_message != null)
		{
			m_ps.print(_message);
		}
		
		m_ps.println();
		m_ps.flush();
	}
	
	public void redirectLogOutput(OutputStream _os)
	{
		m_ps = new PrintStream(_os);
	}

	@Override
	public void shutdown(Core core)
	{
		// TODO Auto-generated method stub
		
	}
}
